package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.GuaranteeSite;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.model.BoxVo;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.GuaranteeSiteService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sys/guarantee/site")
@Api(tags = "担保公司站点控制器")
public class SysGuaranteeSiteController {


    @Autowired
    private GuaranteeSiteService guaranteeSiteService;


    @ApiOperation(value = "分页查询站点公司")
    @GetMapping
    public JsonResult<Page<GuaranteeSite>> findByPage(Page<GuaranteeSite> page, String name, Byte status) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<GuaranteeSite> guaranteeSitePage = guaranteeSiteService.findByPage(page, shopId, name, status);
        return JsonResult.ok(guaranteeSitePage);
    }

    @ApiOperation(value = "根据ID查询站点公司")
    @GetMapping("{id}")
    public JsonResult<GuaranteeSite> findGuaranteeSite(@PathVariable Long id) {
        GuaranteeSite guaranteeSite = guaranteeSiteService.getById(id);
        return JsonResult.ok(guaranteeSite);
    }


    @ApiOperation(value = "添加站点公司")
    @PostMapping
    public JsonResult addGuaranteeSite(@RequestBody GuaranteeSite guaranteeSite) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        int count = guaranteeSiteService.count(new LambdaQueryWrapper<GuaranteeSite>()
                .eq(GuaranteeSite::getName, guaranteeSite.getName()));
        if (count > 0) {
            throw new ApiException(guaranteeSite + "站点已经存在");
        }
        guaranteeSite.setId(IdWorker.getId());
        guaranteeSite.setShopId(shopId);
        guaranteeSite.setStatus((byte) 1);
        guaranteeSite.setCreated(new Date());
        guaranteeSite.setCreateBy(SecurityUtils.getUserId());
        boolean save = guaranteeSiteService.save(guaranteeSite);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改站点公司")
    @PutMapping
    public JsonResult updateGuaranteeSite(@RequestBody GuaranteeSite guaranteeSite) {
        int count = guaranteeSiteService.count(new LambdaQueryWrapper<GuaranteeSite>()
                .eq(GuaranteeSite::getName, guaranteeSite.getName()));
        if (count > 1) {
            throw new ApiException(guaranteeSite + "站点已经存在");
        }
        boolean update = guaranteeSiteService.updateById(guaranteeSite);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置站点公司状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody GuaranteeSite guaranteeSite) {
        boolean save = guaranteeSiteService.updateById(guaranteeSite);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除站点公司")
    @DeleteMapping("{id}")
    public JsonResult delGuaranteeSite(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = guaranteeSiteService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "查询站点公司下拉框")
    @GetMapping("selectBox/{status}")
    public JsonResult selectBox(@PathVariable Byte status, Long companyId) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<GuaranteeSite> list = guaranteeSiteService.list(new LambdaQueryWrapper<GuaranteeSite>()
                .eq(shopId != 1, GuaranteeSite::getShopId, shopId)
                .eq(GuaranteeSite::getStatus, status)
                .eq(companyId != null, GuaranteeSite::getCompanyId, companyId));
        List<BoxVo> boxs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (GuaranteeSite guaranteeSite : list) {
                boxs.add(new BoxVo(guaranteeSite.getId(), guaranteeSite.getName()));
            }
        }
        return JsonResult.ok(boxs);
    }

    @ApiOperation(value = "查询站点公司下拉框")
    @GetMapping("list")
    public JsonResult list(Byte status, @RequestParam Long companyId) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<GuaranteeSite> list = guaranteeSiteService.list(new LambdaQueryWrapper<GuaranteeSite>()
                .eq(shopId != 1, GuaranteeSite::getShopId, shopId)
                .eq(GuaranteeSite::getCompanyId, companyId)
                .eq(status != null, GuaranteeSite::getStatus, status));

        return JsonResult.ok(list);
    }


}
